package com.raysdata.smartcity.dao.sys;

import com.raysdata.smartcity.HelperClazz;
import com.raysdata.smartcity.dao.RzBaseDao;
import com.raysdata.smartcity.dto.Role;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * Created by liux on 2016/10/14.
 */
@Component
public class RoleDao {
    Log log = LogFactory.getLog(RoleDao.class);
    @Autowired
    private RzBaseDao rzBaseDao;
    @Autowired
    private JdbcTemplate jdbcTemplate;

    /**
     * 根据ID查询角色
     * @param role_id
     * @return
     */
    public Role getRoleById(String role_id) {
        final String sql ="select role_id,name,remark,status   from ss_role where role_id = ?";
        List<Role> role = rzBaseDao.query(sql,Role.class,new Object[]{role_id});
        return role.isEmpty() ? null : role.get(0);
    }

    /**
     * '查询所有角色
     * @return
     */
    public List<Role> queryAllRoles(){
        String sql="select role_id,name from ss_role where status=0 ";
        return rzBaseDao.query(sql,Role.class);
    }

    /**
     * 查询所有角色--分页
     * @param page
     * @param count
     * @param role
     * @return
     */
    public List<Role> queryRoles( Integer page, Integer count,Role role) {
        StringBuilder sb=new StringBuilder();
        sb.append(" select * from ss_role where 1=1 ");
        if (!HelperClazz.isEmpty(role.getName())) {
            sb.append(" and name like '%" + role.getName() + "%' ");
        }

        Object[] params = new Object[0];
        int index = 0;
        Object[] paramsEnd = new Object[index];
        System.arraycopy(params, 0, paramsEnd, 0, index);
        if (page <= 0 || count <= 0) {//all
            return rzBaseDao.query(sb.toString(), Role.class, paramsEnd);
        } else {
            count = (count > 0) ? count : Integer.MAX_VALUE;
            return rzBaseDao.queryBySlice(sb.toString(), Role.class, paramsEnd, page, count);
        }
    }

    /**
     * 新增角色
      * @param role
     * @return
     */
    public int addRole(Role role) {
        try {

            final StringBuilder sql = new StringBuilder();
            sql.append("insert into ss_role  ( role_id,  ");
            sql.append("name , ");
            sql.append("remark,                   ");
            sql.append("status                    ");

            sql.append(") values(       ");
            sql.append(" '" + UUID.randomUUID().toString().trim().replaceAll("-", "") + "',         ");
            sql.append(":name,                   ");
            sql.append(":remark,                   ");
            sql.append("'0'                  ");
            sql.append(")");

            rzBaseDao.insert(sql.toString(), role);
            return 0;
        } catch (Exception e) {
            return -1;
        }

    }

    /**
     * 修改角色
     * @param role
     * @return
     */
    public int updRole(Role role) {
        try {
            StringBuilder sql = new StringBuilder();
            sql.append("UPDATE ss_role                        ");
            sql.append("	SET name=:name,remark=:remark   ");
            sql.append("	WHERE role_id=:role_id");
            rzBaseDao.update(sql.toString(), role);
            return 0;

        } catch (Exception e) {
            return -1;
        }
    }

    /**
     * 删除角色
     * @param id
     */
    public void delRoleById(String id){
        String sql = "  delete from  ss_role where role_id='"+id+"'";
        jdbcTemplate.execute(sql);
    }
    /**
     * 获取用户所有角色
     * @param role_id
     * @return
     */
    public List<Map<String,Object>> queryUserByRoleId(String role_id) {
        String sql =" select ur.user_id from ss_user_role ur where ur.role_id ='"+role_id+"'";
        List<Map<String,Object>> list=new ArrayList<Map<String,Object>>();
        list=jdbcTemplate.queryForList(sql);
        return list;
    }

    public Role getRoleByName(String name){
        final String sql ="select role_id,name,remark from ss_role where name = ?";
        List<Role> role = rzBaseDao.query(sql,Role.class,new Object[]{name});
        return role.isEmpty() ? null : role.get(0);
    }

    public List<Map<String,Object>> queryMenuByRoleId(String role_id) {
        String sql =" select ur.menu_id as menuId,r.name as menuName from ss_role_menu ur LEFT JOIN  ss_menu r ON ur.menu_id=r.menu_id  where role_id ='"+role_id+"'";
        List<Map<String,Object>> list=new ArrayList<Map<String,Object>>();
        list=jdbcTemplate.queryForList(sql);
        return list;
    }


    public void delRoleMenus(String role_id){
        String sqlu = "delete from  ss_role_menu where role_id= '"+role_id+"'";
        jdbcTemplate.execute(sqlu);
    }

    public void configMenus(String role_id,String menu_id){
        String sql =  " insert into ss_role_menu(id,role_id,menu_id) values('" + UUID.randomUUID().toString().trim().replaceAll("-", "") + "','"+role_id+"','"+menu_id+"') ";
        jdbcTemplate.execute(sql);
    }


}
